package org.net.cms.service.student;

import jakarta.annotation.Resource;
import org.net.cms.controller.admin.student.vo.StudentReqVO;
import org.net.cms.dao.StudentDto;
import org.net.cms.mapper.ParentAccountMapper;
import org.net.cms.mapper.StudentMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService{

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private ParentAccountMapper parentAccountMapper;


    @Override
    public Boolean update(StudentReqVO studentReqVO) {
        studentMapper.updateStudent(studentReqVO);
        return Boolean.TRUE;
    }

    @Override
    public Boolean delete(int id) {
        studentMapper.deleteStudent(id);
        return Boolean.TRUE;
    }

    @Override
    public StudentDto findById(int id) {
        return studentMapper.getStudent(id);
    }

    @Override
    public List<StudentDto> findAll() {
        return studentMapper.findAll();
    }

    @Override
    public List<StudentDto> findMyStudents(int id) {
        String myStudentIds = parentAccountMapper.getMyStudentById(id);

        if (myStudentIds == null || myStudentIds.trim().isEmpty()) {
            return List.of();
        }

        String[] ids = myStudentIds.split(",");
        List<StudentDto> list = new ArrayList<>();
        for (String getId : ids) {
            int stuId = Integer.parseInt(getId);
            list.add(studentMapper.getStudent(stuId));
        }

        return list;
    }

    @Override
    public int getParentId(String phone) {
        return parentAccountMapper.getParentAccountByPhone(phone).getId();
    }
}
